<?php
/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 */

/**
 * Application Controller
 * 
 * @author Rodrigo Rutkoski Rodrigues <rutkoski@gmail.com>
 * @package Simplify_Kernel_Controller
 */
class ApplicationControllerImpl implements IApplicationController
{

  /**
   * 
   * @var Config
   */
  protected $config;

  /**
   * 
   * @var IRequest
   */
  protected $request;

  /**
   * 
   * @var IResponse
   */
  protected $response;

  /**
   * 
   * @var IRouter
   */
  protected $router;

  /**
   * Constructor.
   * 
   * @param Config $config
   * @return void
   */
  public function __construct(Config $config)
  {
    $this->config = $config;
    
    $this->config->load(SY_DIR . '/config/core-yaml.php');
    
    $class = $config->get('core:router:class', 'AutoRouterImpl');
    $this->router = new $class($this);
    
    $class = $config->get('core:request:class', 'RequestImpl');
    $this->request = new $class($this);
    
    $class = $config->get('core:response:class', 'ResponseImpl');
    $this->response = new $class($this);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/api/IApplicationController#dispatch()
   */
  public function dispatch()
  {
    $route = $this->request->get('route');
    
    $cache = new FileCache($this->config->get('cache:dir'));
    $cacheid = $this->config->get('locale') . $route;
    
    if ($this->config->get('cache:enabled', true) && $cache->cached($cacheid)) {
      $output = $cache->read($cacheid);
    }
    
    else {
      $this->request->parse($route);
      
      $Controller = $this->factory($this->request->get('controller'), $this->request->get('path'));
      $output = $Controller->callActionArray($this->request->get('action'), $this->request->get('params'));
      
      if ($this->config->get('cache:enabled', true) && $Controller->cacheEnabled) {
        $cache->write($cacheid, $output, $Controller->cacheTimeout);
      }
    }
    
    return $this->response->output($output);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/api/IApplicationController#getConfig()
   */
  public function getConfig()
  {
    return $this->config;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/api/IApplicationController#getRouter()
   */
  public function getRouter()
  {
    return $this->router;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/api/IApplicationController#getRequest()
   */
  public function getRequest()
  {
    return $this->request;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/api/IApplicationController#getResponse()
   */
  public function getResponse()
  {
    return $this->response;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/core/IApplicationController#forward($route)
   */
  public function forward($route)
  {
    return $this->response->redirect($route);
  }

  /**
   * Factory an Action Controller object.
   * 
   * @param string $name controller name
   * @return ActionController
   */
  protected function factory($controller, $path)
  {
    $class = Inflector::camelize($controller . '_controller');
    $Controller = new $class($this);
    $Controller->init($path);
    return $Controller;
  }

}

?>